#include <hd_led.h>
#include <hd_oled.h>
#include <hd_oled_protocol_i2c.h>
#include <stdio.h>
#include <pd_usart.h>
#include <pd_i2c_hd.h>
#include "main.h"

#define ARRAY_SIZE(x)       sizeof(x) / sizeof(x[0])
#define USE_HARDWARE_SPI    1
#define USE_HARDWARE_I2C    1
uint16_t num;
LED led;
OLED oled;

extern void SystemClock_Config(void);

void oled_instance(OLED *oled);


int main(void) {
    SystemClock_Config();

    NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);

    oled_instance(&oled);

    OLED_printf(&oled, 0, 0, "USART1 Send");
    OLED_refresh(&oled);

    // USART1 初始化
    //    PD_H_USART1_Init();

    GPIO usartGPIO[2];
    GPIO_constructor(usartGPIO + 0, GPIOA, LL_GPIO_PIN_9);
    GPIO_constructor(usartGPIO + 1, GPIOA, LL_GPIO_PIN_10);

    GPIOList uartGPIOList;
    GPIOList_constructor(&uartGPIOList, usartGPIO, 2);

    USART usart;
    USART_constructor(&usart, USART1, &uartGPIOList, 9600);

    USART_init(&usart, USART1_IRQn, 1, 1);

    // 发送字节
    usart.vptr->send_byte(&usart, 0x41);

    // 发送数组
    uint8_t array[] = {0x42, 0x43, 0x44, 0x45, 0xD, 0xA};
    usart.vptr->send_array(&usart, array, 6);
    // 发送字符串、数字
    USART_sendString(&usart, "Num1=");
    usart.vptr->send_num(&usart, 111, 3);
    USART_sendString(&usart, "\r\n");

    printf("Num2=%d\r\n", 222);

    // 发送字符串
    char String[100];
    sprintf(String, "Num3=%d\r\n", 333);
    USART_sendString(&usart, String);

    // printf
    USART_printf(&usart, "Num4=%d\r\n", 444);
    USART_printf(&usart, "\r\n");

    while (1) {
    }
}


void oled_instance(OLED *oled) {
    // I2C gpio init
    static GPIO oledSckGPIO, oledSdaGPIO;
    GPIO_constructor(&oledSckGPIO, GPIOB, LL_GPIO_PIN_10); // I2Cx_SCK
    GPIO_constructor(&oledSdaGPIO, GPIOB, LL_GPIO_PIN_11); // I2Cx_SDA

#ifdef USE_HARDWARE_I2C
    static I2CHardware i2c2;
    I2CHardware_constructor(&i2c2, I2C2, &oledSckGPIO, &oledSdaGPIO);
#elif USE_SIMULATE_I2C
    static I2CSimulate i2c2;
    I2CSimulate_constructor(&i2c2, &oledSckGPIO, &oledSdaGPIO);
#endif
    I2C_init((I2C *)&i2c2);

    // OLED protocol of I2C
    static OLEDProtocolI2C oledProtocolI2c;
    OLEDProtocolI2C_constructor(&oledProtocolI2c, (I2C *)&i2c2);

    // OLED font instance.
    static OLEDFont oledFont;
    OLEDFont_contructor(&oledFont, NULL);

    // OLED instance of 4-PIN(I2C)
    OLED_constructor(oled, (OLEDProtocol *)&oledProtocolI2c, &oledFont);

    OLED_init(oled);

    OLED_clear(oled);
    OLED_refresh(oled);
}
